(function() {

    angular.module('app').component('toolBarComponent', {
        css: 'app/components/tool.bar.component/tool.bar.component.css',
        templateUrl: 'app/components/tool.bar.component/tool.bar.component.html',
        controller: toolBarComponent,
        controllerAs: 'vm',
        bindings: {
            bars: '<'
        }
    });

    toolBarComponent.$inject = ['$timeout'];

    function toolBarComponent($timeout) {

        var vm = this;
        /*
                
        demo--
        vm.bars = [
            {
                text: '店铺数',
                default:'店铺数',
                sortColumn:'shopNum',
                sort:'',
                click: function() {

                    if(this.text === '店铺数'){

                        this.text = "店铺数从低到高";
                        this.sort = "up";          

                    }else if(this.text === "店铺数从低到高"){

                        this.text = "店铺数从高到低"; 
                        this.sort = "down";  
                                

                    }else{
                        this.text = "店铺数从低到高";

                        this.sort = "up";
                        
                      
                    }
                    $rootScope.$emit('tool-bar-component-$emit-EV',{'sortColumn':'shopNum','sort':this.sort});

                    vm.bars.init(this);
                    
                }

            },{
                text: '宝贝数',
                default:'宝贝数',
                sortColumn:'productNum',
                sort:'',
                click: function() {

                    if(this.text === '宝贝数'){

                        this.text = "宝贝数从低到高";
                        this.sort = "up";          

                    }else if(this.text === "宝贝数从低到高"){

                        this.text = "宝贝数从高到低"; 
                        this.sort = "down";  
                                

                    }else{
                        this.text = "宝贝数从低到高";

                        this.sort = "up";
                        
                      
                    }
                    $rootScope.$emit('tool-bar-component-$emit-EV',{'sortColumn':'productNum','sort':this.sort});

                    vm.bars.init(this);
                    
                }

            },{
                text: '销售额',
                default:'销售额',
                sortColumn:'sellMoney',
                sort:'',
                click: function() {

                    if(this.text === '销售额'){

                        this.text = "销售额从低到高";
                        this.sort = "up";          

                    }else if(this.text === "销售额从低到高"){

                        this.text = "销售额从高到低"; 
                        this.sort = "down";  
                                

                    }else{
                        this.text = "销售额从低到高";

                        this.sort = "up";
                        
                      
                    }
                    $rootScope.$emit('tool-bar-component-$emit-EV',{'sortColumn':'sellMoney','sort':this.sort});

                    vm.bars.init(this);
                    
                }

            },{
                text: '销量',
                default:'销量',
                sortColumn:'sellAmount',
                sort:'',
                click: function() {

                    if(this.text === '销量'){

                        this.text = "销量从低到高";
                        this.sort = "up";                    

                    }else if(this.text === "销量从低到高"){

                        this.text = "销量从高到低"; 
                        this.sort = "down";                     

                    }else{

                        this.text = "销量从低到高";
                        this.sort = "up";
                        
                    }

                    $rootScope.$emit('tool-bar-component-$emit-EV',{'sortColumn':'sellMoney','sort':this.sort});

                    vm.bars.init(this);
                    
                }

            }, {
                type: 'dropdown',
                text: '价格',
                icon: 'fa fa-angle-down',
                show: false,
                menu_item: [{
                    text: '价格从低到高',
                    value: 'taobao',
                    icon: ''
                }, {
                    text: '价格从高到低',
                    value: 'taobao',
                    icon: ''
                }],
                onSelect: function(item,event) {

                    console.log('select:');

                    this.text = item.text;

                    alertM(item.text);

                    this.show = false;

                    event.stopPropagation();
                    event.preventDefault();
                },
                click: function() {
                    this.show = !this.show;
                }

            }, {

                type: 'dropdown',
                text: '全网',
                name: 'shopType',
                class: 'clearfix',
                icon: 'fa fa-angle-down',
                show: false,
                menu_item: [{
                    text: '全网',
                    value: 'taobao',
                    icon: ''
                }, {
                    text: '淘宝',
                    value: 'taobao',
                    icon: ''
                }, {
                    text: '天猫',
                    value: 'taobao',
                    icon: ''
                }, {
                    text: '全球购',
                    value: 'taobao',
                    icon: ''
                }, {
                    text: '天猫过几',
                    value: 'taobao',
                    icon: ''
                }],

                onSelect: function(item,event) {

                    console.log('select:');

                    this.text = item.text;

                    alertM(item.text);

                    this.show = false;

                    event.stopPropagation();
                    event.preventDefault();
                },
                click: function() {
                    this.show = !this.show;
                }
            }
        ];
        */



        vm.$onInit = function() {
            console.log(vm.bars);


            //li set active
            vm.bars.setActive = function($index, event, navItem) {

                console.log('setActive');

                this._index = $index;

                var type = navItem.type;

                event.stopPropagation();
                event.preventDefault();

                navItem.click();


            };
            //li remove active
            vm.bars.delActive = function() {
                console.log('del ooo');
                if (this._index === -1) {
                    return;
                }
                this._index = -1;
            };

            //init
            vm.bars.init = function(barItem) {

                var current_index = vm.bars.indexOf(barItem);

                console.log(this);
                for (var i = 0; i < vm.bars.length; i++) {

                    if (vm.bars[i].default && i !== current_index) {
                        vm.bars[i].text = vm.bars[i].default;
                    }
                }

                i = null;
            };

            //copy
            // vm.bars.copy = angular.copy(vm.bars);




        }


    }

})();
